package GUI_Page;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import Model.Announce;
import Model.User;

public class HotAnnounces extends JPanel {

	private DefaultListModel jListItem;
	private JLabel jLabel1;
	private JList jList;
	private JScrollPane jScrollPane1;
	private User user;
	private ArrayList<Announce> announces;
	
	public HotAnnounces() {
		initComponents();
	}
	
	public HotAnnounces(User user)
	{
		this.user=user;		
		this.initComponents();
		this.addListener();
		this.loadAnnounces();
	}
	
	private void initComponents() {
		this.jListItem = new DefaultListModel();;
		jScrollPane1 = new JScrollPane();
		jList = new JList(jListItem);
		jLabel1 = new JLabel();

		jList.setBorder(BorderFactory
				.createBevelBorder(BevelBorder.RAISED));
		jList.setVisibleRowCount(10);
		jScrollPane1.setViewportView(jList);

		jLabel1.setText("\u8fd1\u671f\u901a\u77e5");

		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup().addComponent(jLabel1)
								.addContainerGap())
				.addComponent(jScrollPane1,
						GroupLayout.DEFAULT_SIZE, 183,
						Short.MAX_VALUE));
		layout.setVerticalGroup(layout
				.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addComponent(jLabel1)
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED,
										GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addComponent(jScrollPane1,
										GroupLayout.PREFERRED_SIZE,
										220,
										GroupLayout.PREFERRED_SIZE)));
	}
	
	private void addListener() {
		jList.addMouseListener(new MouseAdapter(){
			 public void mouseClicked(MouseEvent evt) {
				 annListJListMouseClickPerform(evt);
		        }
		});
	}

	protected void annListJListMouseClickPerform(MouseEvent evt) {
		int i=jList.getSelectedIndex();
		JOptionPane.showMessageDialog(null,user.Announces.get(i).Content,user.Announces.get(i).Title,JOptionPane.PLAIN_MESSAGE);
	}

	private void loadAnnounces() {
		this.announces = user.Announces;
		String temp;
		if(announces.size()<=10)
		{
			for(int i=0;i<announces.size();i++)
			{
				temp = this.user.Info.Name+"    "+announces.get(i).Time.toString()+"    "+announces.get(i).Title;
				jListItem.add(i,temp);
			}
		}else
		{
			for(int i=0;i<10;i++)
			{
				temp=this.user.Info.Name+" "+announces.get(i).Time.toString()+"    "+announces.get(i).Title;
				jListItem.add(i,temp);
			}
		}	
	}
}